package example1;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
/**
 * @author colin.cheng
 * @version V1.0
 * @date Created In 14:51 2019/9/11
 */
public class demo extends JFrame implements KeyListener{

    long pressTime=0;
    long releasedTime=0;
    boolean keyEnable=true;

    public static void main(String[] args) {
        demo ket = new demo();
    }

    public demo() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setTitle("KeyEventTest");
        this.setSize(300, 300);
        this.setVisible(true);
        addKeyListener(this);
    }

    public void keyPressed(KeyEvent e) {
        if (keyEnable ==true) {
            pressTime = System.currentTimeMillis();
            keyEnable=false;
            System.out.printf("keyTyped code=%s,char=%s\n", e.getKeyCode(), e.getKeyChar());
            System.out.println("按下");
        }
    }

    public void keyReleased(KeyEvent e) {
        releasedTime=System.currentTimeMillis();
        long afterTime=releasedTime-pressTime;
        System.out.println("总时间"+afterTime+"毫秒");
        keyEnable=true;
    }

    public void keyTyped(KeyEvent e) {
    }

}
